﻿namespace QLPhongKham.BaseClass.VienPhi
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtovpDMQuyenSo
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private int _dedenso;
        private int _deid;
        private int _desodangghi;
        private int _detuso;
        private int _iiddangnhap;
        private int _isudung;
        private string _skihieu;
        private string _sngaycn;
        private string _sSchema;
        private string _sten;
        private string _stenmaytinh;

        public dtovpDMQuyenSo()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtovpDMQuyenSo(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtovpDMQuyenSo.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { "delete from ", this._sSchema, ".", tableName, " where 1=1   and ", columnID, " = ", this._deid }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtovpDMQuyenSo.cs");
                return false;
            }
        }

        public int GetIDInsert()
        {
            int iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "dtovpDMQuyenSo.cs");
                iDInsert = 1;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public int GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                int num = 0;
                try
                {
                    num = int.Parse(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return ++num;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "dtovpDMQuyenSo.cs");
                return 1;
            }
        }

        public int GetSoBienLai(int idquyenso)
        {
            int soBienLai;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                soBienLai = this.GetSoBienLai(this._connect, idquyenso);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetSoBienLai()", "dtovpDMQuyenSo.cs");
                soBienLai = 1;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return soBienLai;
        }

        public int GetSoBienLai(NpgsqlConnection connect, int idquyenso)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(string.Concat(new object[] { " select max(", columnSODANGGHI, ") as idmax  from ", this._sSchema, ".", tableName, " where ", columnID, "=", idquyenso }), connect).Fill(dataTable);
                int num = 0;
                try
                {
                    num = int.Parse(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return ++num;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetSoBienLai(NpgsqlConnection connect)", "dtovpDMQuyenSo.cs");
                return 1;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtovpDMQuyenSo.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnID, ",", columnTUSO, ",", columnDENSO, ",", columnSODANGGHI, ",", columnKIHIEU, ",", columnSUDUNG, 
                    ",", columnIDDANGNHAP, ",", columnTENMAYTINH, ",", columnTEN, ") values(", this._deid, ",", this._detuso, ",", this._dedenso, ",", this._desodangghi, ",'", this._skihieu, 
                    "',", this._isudung, ",", this._iiddangnhap, ",:", columnTENMAYTINH, ",:", columnTEN, ")"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                command.Parameters.Add(columnTEN, NpgsqlDbType.Text, 200).Value = this._sten;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtovpDMQuyenSo.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._deid = 0;
            this._detuso = 0;
            this._dedenso = 0;
            this._desodangghi = 0;
            this._skihieu = "";
            this._isudung = 0;
            this._iiddangnhap = 0;
            this._stenmaytinh = "";
            this._sngaycn = "";
            this._sten = "";
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtovpDMQuyenSo.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                string str = "";
                if (this._skihieu != "")
                {
                    string str3 = str;
                    str = str3 + " and " + columnKIHIEU + "='" + this._skihieu + "'";
                }
                if (this._deid != 0)
                {
                    object obj2 = str;
                    str = string.Concat(new object[] { obj2, " and ", columnID, "=", this._deid });
                }
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnTUSO + "," + columnDENSO + "," + columnSODANGGHI + "," + columnKIHIEU + "," + columnSUDUNG + "," + columnIDDANGNHAP + "," + columnTENMAYTINH + ",to_char(" + columnNGAYCN + ",'dd/mm/yyyy') as " + columnNGAYCN + "," + columnTEN + " from " + this._sSchema + "." + tableName + " where " + ((str != "") ? (" 1=1 " + str) : " 1=0"), connect).Fill(dataTable);
                if (dataTable.Rows.Count != 0)
                {
                    this._deid = int.Parse(dataTable.Rows[0][columnID].ToString());
                    this._detuso = int.Parse(dataTable.Rows[0][columnTUSO].ToString());
                    this._dedenso = int.Parse(dataTable.Rows[0][columnDENSO].ToString());
                    this._desodangghi = int.Parse(dataTable.Rows[0][columnSODANGGHI].ToString());
                    this._skihieu = dataTable.Rows[0][columnKIHIEU].ToString();
                    this._isudung = int.Parse(dataTable.Rows[0][columnSUDUNG].ToString());
                    this._iiddangnhap = int.Parse(dataTable.Rows[0][columnIDDANGNHAP].ToString());
                    this._stenmaytinh = dataTable.Rows[0][columnTENMAYTINH].ToString();
                    this._sngaycn = dataTable.Rows[0][columnNGAYCN].ToString();
                    this._sten = dataTable.Rows[0][columnTEN].ToString();
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtovpDMQuyenSo.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtovpDMQuyenSo.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnTUSO + "," + columnDENSO + "," + columnSODANGGHI + "," + columnKIHIEU + "," + columnSUDUNG + "," + columnIDDANGNHAP + "," + columnTENMAYTINH + ",to_char(" + columnNGAYCN + ",'dd/mm/yyyy hh24:mi:ss') as " + columnNGAYCN + "," + columnTEN + " from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtovpDMQuyenSo.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtovpDMQuyenSo.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnTUSO, " = ", this._detuso, ",", columnDENSO, " = ", this._dedenso, ",", columnSODANGGHI, " = ", this._desodangghi, 
                    ",", columnKIHIEU, " = '", this._skihieu, "',", columnSUDUNG, " = ", this._isudung, ",", columnIDDANGNHAP, " = ", this._iiddangnhap, ",", columnTENMAYTINH, " = :", columnTENMAYTINH, 
                    ",", columnNGAYCN, " = now() ,", columnTEN, "=:", columnTEN, " where ", columnID, " = ", this._deid
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                command.Parameters.Add(columnTEN, NpgsqlDbType.Text, 200).Value = this._sten;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtovpDMQuyenSo.cs");
                return false;
            }
        }

        public static string columnDENSO
        {
            get
            {
                return "denso";
            }
        }

        public static string columnID
        {
            get
            {
                return "id";
            }
        }

        public static string columnIDDANGNHAP
        {
            get
            {
                return "iddangnhap";
            }
        }

        public static string columnKIHIEU
        {
            get
            {
                return "ma";
            }
        }

        public static string columnNGAYCN
        {
            get
            {
                return "ngaycn";
            }
        }

        public static string columnSODANGGHI
        {
            get
            {
                return "sodangghi";
            }
        }

        public static string columnSUDUNG
        {
            get
            {
                return "sudung";
            }
        }

        public static string columnTEN
        {
            get
            {
                return "ten";
            }
        }

        public static string columnTENMAYTINH
        {
            get
            {
                return "tenmaytinh";
            }
        }

        public static string columnTUSO
        {
            get
            {
                return "tuso";
            }
        }

        public string NGAYCN
        {
            get
            {
                return this._sngaycn;
            }
            set
            {
                this._sngaycn = value;
            }
        }

        public int pDENSO
        {
            get
            {
                return this._dedenso;
            }
            set
            {
                this._dedenso = value;
            }
        }

        public int pID
        {
            get
            {
                return this._deid;
            }
            set
            {
                this._deid = value;
            }
        }

        public int pIDDANGNHAP
        {
            get
            {
                return this._iiddangnhap;
            }
            set
            {
                this._iiddangnhap = value;
            }
        }

        public string pKIHIEU
        {
            get
            {
                return this._skihieu;
            }
            set
            {
                this._skihieu = value;
            }
        }

        public int pSODANGGHI
        {
            get
            {
                return this._desodangghi;
            }
            set
            {
                this._desodangghi = value;
            }
        }

        public bool pSUDUNG
        {
            get
            {
                return (this._isudung == 1);
            }
            set
            {
                this._isudung = value ? 1 : 0;
            }
        }

        public string pTEN
        {
            get
            {
                return this._sten;
            }
            set
            {
                this._sten = value;
            }
        }

        public string pTENMAYTINH
        {
            get
            {
                return this._stenmaytinh;
            }
            set
            {
                this._stenmaytinh = value;
            }
        }

        public int pTUSO
        {
            get
            {
                return this._detuso;
            }
            set
            {
                this._detuso = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "vp_dmquyenso";
            }
        }
    }
}

